Eureka基本使用 结合feign
eureka Server 服务注册与发现服务器代码 spring boot 2.0.4
1、pom springBoot 2.0.4,同样属于springBoot-web spring-cloud...
...
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2、单节点服务注册中心 yml 配置
server:
port: 8769
eureka:
instance:
hostName: localhost
client:
registerWithEureka: false #默认true,不许要注册自己
fetchRegistry: false #单节点,不需要同步其他注册中心
service-url:
defaultZone: http://${eureka.instance.hostName}:${server.port}/eureka/
3、启动代码
package com.gy.sc.discoveryeureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 开启注册中心server
public class DiscoveryEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryEurekaApplication.class, args);
}
}
4、验证server启动结果
服务提供者代码
1、pom依赖 sb版本统一2.0.4 web项目 spring-cloud...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
// mysql数据看访问依赖、jpa...
2、yml配置 注意:eureka配置跟spring相关配置同级!
server:
port: 8899
spring:
application:
name: sc-provider-user # 应用服务名称
datasource:
username: root
password: FFhcc01180325
url: jdbc:mysql://127.0.0.1:3306/wisdom
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
enable_lazy_load_no_trans: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 10
register-with-eureka: true
3、服务暴露
package com.gy.sc.scprovideruser.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.gy.sc.scprovideruser.entity.TUser;
import com.gy.sc.scprovideruser.repository.TUserRepository;
@RestController
public class TUserController {
@Autowired
private TUserRepository tUserRepository;
/**
* @author GY
* @date 2018年10月29日
* @说明:getOne是返回一个实体的引用——代理对象,findOne是返回实体
* Jpa自动适配了数据库表跟实体的驼峰映射
*/
@GetMapping("/get/user/{id}")
public TUser getUserById(@PathVariable("id") Integer id) {
// 使用findOne不会报错,使用getOne 如果TUser类上不加 @JsonIgnoreProperties... 就会报错
// 见:https://blog.****.net/gw816/article/details/80401284#commentBox
Example<TUser> example = Example.of(new TUser().setId(id));
Optional<TUser> findOne = tUserRepository.findOne(example);
if(findOne.isPresent())
return findOne.get();
return null;
}
@GetMapping("/add/user")
public TUser addUser(TUser tUser) {
TUser user = tUserRepository.save(tUser);
return user;
}
}
entity...repository...
4、启动类
package com.gy.sc.scprovideruser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ScProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ScProviderUserApplication.class, args);
}
}
服务调用者代码(可能成为提供者,也需要注册自己的服务)
RestTemplate使用
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
调用
@Autowired
private RestTemplate restTemplate;
@GetMapping("movie/get/user2/{id}")
public TUser findAUserById2(@PathVariable("id") Integer id) {
// 服务名称必须用大写!!!
TUser tUser2 = restTemplate.getForObject(
"http://SC-PROVIDER-USER/get/user/" + id, TUser.class);
System.out.println(tUser2);
return tUser2;
}
结果
feign使用
1、pom依赖,整合feign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
2、yml配置
server:
port: 8800
spring:
application:
name: sc-consumer-movie
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8769/eureka/
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 10
register-with-eureka: true
3、remote(应该可以跟服务提供者使用公共controller jar包接口)
package com.gy.sc.scconsumermovie.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gy.sc.scconsumermovie.entity.TUser;
@FeignClient(name = "sc-provider-user")
public interface UserRemote {
@RequestMapping("/get/user/{id}")
public TUser getUserById(@PathVariable("id") Integer id);
}
entity...(跟服务提供者可以共用jar包)
4、启动类
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ScConsumerMovieApplication {
public static void main(String[] args) {
SpringApplication.run(ScConsumerMovieApplication.class, args);
}
}
5、调用注册服务(应该是service层调用)
@Autowired
private UserRemote userRemote;
@GetMapping("movie/get/user/{id}")
public TUser findAUserById(@PathVariable("id") Integer id) {
TUser tUser = userRemote.getUserById(1);
System.out.println(tUser);
return tUser;
}
调用结果