Eureka 注意版本和springboot兼容问题
1、依赖 2、注解 3、配置文件 4、成功注册服务
父工程
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注册eureka服务
启动类注解和配置
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
server:
port: 8761 # 端口号
eureka:
instance:
hostname: localhost # localhost
client:
# 当前的eureka服务是单机版的
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
注册客户服务
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类
@SpringBootApplication
@EnableEurekaClient
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class,args);
}
}
# 指定Eureka服务地址
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
#指定服务的名称
spring:
application:
name: CUSTOMER
测试
@Autowired
private EurekaClient eurekaClient;
安全性
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
类
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 忽略掉/eureka/**
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
服务配置文件
# 指定用户名和密码
spring:
security:
user:
name: root
password: root
其他服务加入用户名密码
eureka:
client:
service-url:
defaultZone: http://用户名:密码@localhost:8761/eureka
高可用性
客户注册到多个eureka服务
eureka:
client:
service-url:
defaultZone: http://root:root@localhost:8761/eureka,http://root:root@localhost:8762/eureka
eureka服务之间相互注册实现集成 8761注册到8762,8762注册到8761
eureka:
client:
registerWithEureka: true # 注册到Eureka上
fetchRegistry: true # 从Eureka拉取信息
serviceUrl:
defaultZone: http://root:root@localhost:8762/eureka/
Ribbon负载均衡
步骤: 1、导入依赖 2、加入注解 3、通过服务名+路径访问
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
--------------------------------------------------------
@GetMapping("/customer")
public String customer(){ //SEARCH是eureka里面服务的名称
String result = restTemplate.getForObject("http://SEARCH/search", String.class);
//4. 返回
return result;
}
负载均衡策略方式
1、注解方式
@Bean
public IRule robbinRule(){
return new RandomRule();
}
2、配置文件配置
# 指定具体服务的负载均衡策略
SEARCH: # 编写服务名称
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule # 具体负载均衡使用的类
Feign代替RestTemplate访问服务
1、导入依赖
2、加入注解
3、创建接口 通过接口访问
4.调用接口的方法 直接访问
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类注解 @EnableFeignClients
创建接口,接口的方法和路径全部和另一个服务的controller一样
@FeignClient("SEARCH") // 指定服务名称
public interface SearchClient {
// value -> 目标服务的请求路径,method -> 映射请求方式
@RequestMapping(value = "/search",method = RequestMethod.GET)
String search();
//FeignClient的接口方法 有参数的时候参数需要加入注解
@RequestMapping("/search/{id}")
public Customer findById(@PathVariable(value = "id") Integer id);
@RequestMapping("/getCustomer")
public Customer getCustomer(@RequestParam("id") Integer id,@RequestParam("name") String name);
//这个方法参数前没有加任何东西,默认就一个@RequestBody注解,所以另一个服务的参数要加@RequestBody
@RequestMapping("/add")
public Customer add(Customer customer);
}
另一个服务的controller
@Value("${server.port}")
private String port;
@RequestMapping("/search/show")
public String show(){
System.out.println("show");
return "search"+port;
}
@RequestMapping("/search/{id}")
public Customer findById(@PathVariable Integer id){
return new Customer(id,"zhangsan",19);
}
@RequestMapping("/getCustomer")
public Customer getCustomer(Integer id, String name){
return new Customer(id,name,30);
}
@RequestMapping("/add")
public Customer add(@RequestBody Customer customer){
return customer;
}
测试
@Autowired
private EurekaClient eurekaClient;
@Autowired
private RestTemplate restTemplate;
@Autowired
private FeignClient feignClient;
@RequestMapping("/client/test")
public Customer test(){
//EurekaClient 和restTemplate方式访问
// InstanceInfo info = eurekaClient.getNextServerFromEureka("Search", false);
// String homePageUrl = info.getHomePageUrl();
// System.out.println(homePageUrl);
// String result = restTemplate.getForObject(homePageUrl + "/search/show", String.class);
String result = restTemplate.getForObject("http://Search/search/show", String.class);
System.out.println(result);
return new Customer(1,"lee",30);
}
-------------------------------------------
@RequestMapping("/client/test")
public String test(){
return feignClient.show();
}
@RequestMapping("/client/test2/{id}")
public Customer test2(@PathVariable Integer id){
return feignClient.findById(id);
}
@RequestMapping("/client/test3")
public Customer test3(Integer id,String name){
return feignClient.getCustomer(id,name);
}
@RequestMapping("/client/test4")
public Customer test4(Customer customer){
System.out.println(customer);
return feignClient.add(customer);
}