Eureka搭建

1.注册中心server端
1.1.引入依赖
复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2.配置
复制代码
server:
  port: 8761
spring:
  application:
    name: eureka-server #服务名称,如多台server,那么保持一致
  security: #security用户名密码
    user:
      name: root
      password: root
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true #设置显示ip地址
    instance-id: ${spring.cloud.client.ipaddress}:${server.port}
  client:
    fetch-registry: false #是否注册自己默认为true,如果
    register-with-eureka: false
    service-url: #注册中心地址  root:root@ 为security用户名密码
      defaultZone: http://root:root@localhost:8761/eureka/ #http://root:root@${eureka.instance.hostname}:${server.port}/eureka/
1.3.server端security配置
复制代码
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter  {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        //解决/eureka下的跨域问题
        http.csrf().ignoringAntMatchers("/eureka/**");
    }

}
1.4.启动类加上
复制代码
@EnableEurekaServer
2.provider服务提供方
2.1.client端依赖
复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2.client配置
复制代码
server:
  port: 7070

spring:
  application:
    name: service-provider #服务名称,多来provider服务名保持一致

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipaddress}:${server.port}
  client:
    service-url:	
      defaultZone: http://root:root@localhost:8761/eureka/ #,http://localhost:8762/eureka/ 注册中心url多个注册中心用","分隔

service-provider: #ribbon配置 service-provider为服务名
  ribbon: #RandomRule为轮询
    NFLoadBlanceRuleClassName: com.netflix.loadblancer.RandomRule
2.3.提供服务接口
复制代码
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @GetMapping("/getUserByName/{name}")
    public User getUserByName(@PathVariable String name){
        log.info("name={}",name);
        return new User("王渝",22);
    }
}
2.4.开启client
复制代码
@EnableEurekaClient //可以不加
3.consumer服务使用方
3.1.依赖
复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.2.配置
复制代码
server:
  port: 9090

spring:
  application:
    name: service-consumer

eureka:
  client:
    service-url: #注册中心向外暴露的地址
      defaultZone: http://root:root@localhost:8761/eureka/ #,http://localhost:8762/eureka/  多台server已逗号分隔
    register-with-eureka: false
    registry-fetch-interval-seconds: 10 
    #表示EurekaClient间隔多久去拉去注册信息 默认30秒
3.3.Controller接口
复制代码
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceFeign userServiceFeign;

    @GetMapping("/name2/{name}")
    public User getUserByName2(@PathVariable String name){
        return  userServiceFeign.getUserName(name);
    }
}
3.4.Service接口(方法一 feign调用)
复制代码
//service-provide为服务提供方的名称
@FeignClient(value = "service-provider") 
public interface UserServiceFeign {
    
	// /user/getUserByName/{name} 为消息提供方的url
    @GetMapping("/user/getUserByName/{name}") 
    User getUserName(@PathVariable(value = "name") String name);


}
3.5Service接口(方法二 RestTemplate)
复制代码
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public User getUserByName(String name) {
        ResponseEntity<User> response = restTemplate.exchange("http://service-provider/user/getUserByName/" + name,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<User>() {
                });

        return response.getBody();
    }
}
相关推荐
都叫我大帅哥10 分钟前
🌊 Redis Stream深度探险:从秒杀系统到面试通关
java·redis
都叫我大帅哥11 分钟前
Redis持久化全解析:从健忘症患者到记忆大师的逆袭
java·redis
程序猿阿越30 分钟前
Kafka源码(一)Controller选举与创建Topic
java·后端·源码
程序无bug35 分钟前
Spring6 当中 Bean 的生命周期的详细解析:有五步,有七步,有十步
java
二川bro38 分钟前
飞算智造JavaAI:智能编程革命——AI重构Java开发新范式
java·人工智能·重构
Q_970956391 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724091 小时前
Maven
java·数据库·maven
程序猿小D1 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
木头没有瓜2 小时前
idea离线安装插件
java·ide·intellij-idea