Eureka +security 服务端 配置、调用

Eureka服务器集群请看这里 Spring Cloud Eureka+Spring Security 服务器集群

创建项目 user

第一步 修改 pom 文件

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- RabbitMQ 连接支持包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!-- spring security 支持包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- jpa 支持包 jpa 类似 mybatis  -->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-data-jpa</artifactId>-->
        <!--</dependency>-->
        <!-- spring boot 引入的 web 开发的基础依赖支持包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- mybatis 支持包 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!-- MyBatis Plus 支持包导入-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- mysql 驱动连接包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- lombok 插件支持 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- spring boot 单元测试依赖引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- rabbitMQ 单元测试依赖引入 -->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- spring security 单元测试依赖引入 -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Spring boot Actuator 监控端点 支持包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- eureka 客户端 依赖引入 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!-- jwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!-- spring session 管理依赖 -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-jdbc</artifactId>
        </dependency>


    </dependencies>
    <!-- 引入 Spring cloud 的依赖-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

第二步 修改 Application.java 文件

@SpringBootApplication
@EnableEurekaClient//启用 注册 eureka 客户端
public class MicroserviceSimplUserApplication {

    @Bean//注册 RestTemplate Bean
    @LoadBalanced//加载 eureka 服务中心的 域表列名
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceSimplUserApplication.class, args);
    }
}

第三步 编写文件 application.yml

server:
  port: 8010
spring:
  application:
    name: microservice-consumer-movie
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    url: jdbc:mysql://localhost:3306/springcloud?useSSL=false&serverTimezone=GMT%2B8&&characterEncoding=utf-8
    username: root
    password: 请填写你自己的密码
    driver-class-name: com.mysql.cj.jdbc.Driver
  security:
    user:
      name: user
      password: 123456
  main:
    allow-bean-definition-overriding: true
eureka:
  client:
    service-url:
      defaultZone: http://user:123456@peer2:8762/eureka/,http://user:123456@peer1:8761/eureka/
  instance:
    prefer-ip-address: true #表示将自己的ip 注册到 Eureka Server 。 默认false ,表示注册微服务所在 操作系统hostname 到 Eureka Server
    metadata-map:
      #自定义的元数据, key/value 都可以随便写
      my-metedata: 来自Movie的自定义元数据
      haha: eureka
mybatis:
  configuration:
    map-underscore-to-camel-case: true
management:
  endpoints:
    web:
      exposure:
        include: "*"  #暴露所有接口
  endpoint:
    shutdown.enabled: true  #允许shutdown
    health.show-details: always #展示详细的health信息
    jolokia:
      enabled: false #允许 jolokia
      config.debug: true
info: # Spring Boot Actuator  info 端口信息配置  访问 /info 时展示的数据
  app: @project.artifactId@
  encoding: @project.build.sourceEncoding@
  java:
    source: @java.version@
    target: @java.version@

第四步 创建一个user服务 我这里是直接用mybatis-plus 做了一个 根据用户id 查询用户的操作 controller 如下

@RestController
@Slf4j
public class UserController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    public UserEntity findById(@PathVariable Long id){
        log.info("id:{}",id);
        return userService.getById(id);
    }
}

第五步启动 eureka服务器 并启动 user项目

访问 http://peer1:8761 并登陆页面
可以看到如下信息

user服务注册后主页.jpg

以上 一个服务提供者就算完事了 下面写 服务消费者

创建项目 movie

重复 上面一至二步

重复第三步 修改信息如下

server:
  port: 8010
spring:
  application:
    name: microservice-consumer-movie
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    url: jdbc:mysql://localhost:3306/springcloud?useSSL=false&serverTimezone=GMT%2B8&&characterEncoding=utf-8
    username: root
    password: 3sybseri5fb
    driver-class-name: com.mysql.cj.jdbc.Driver
  security:
    user:
      name: user
      password: 123456
  main:
    allow-bean-definition-overriding: true
eureka:
  client:
    service-url:
      defaultZone: http://user:123456@peer2:8762/eureka/,http://user:123456@peer1:8761/eureka/
  instance:
    prefer-ip-address: true #表示将自己的ip 注册到 Eureka Server 。 默认false ,表示注册微服务所在 操作系统hostname 到 Eureka Server
    metadata-map:
      #自定义的元数据, key/value 都可以随便写
      my-metedata: 来自Movie的自定义元数据
      haha: eureka
mybatis:
  configuration:
    map-underscore-to-camel-case: true
management:
  endpoints:
    web:
      exposure:
        include: "*"  #暴露所有接口
  endpoint:
    shutdown.enabled: true  #允许shutdown
    health.show-details: always #展示详细的health信息
    jolokia:
      enabled: false #允许 jolokia
      config.debug: true
info: # Spring Boot Actuator  info 端口信息配置  访问 /info 时展示的数据
  app: @project.artifactId@
  encoding: @project.build.sourceEncoding@
  java:
    source: @java.version@
    target: @java.version@

第六步 在 movie中 创建 一个 消费服务 的controller 代码如下

import com.bxr.microservicesimpleconsumermovie.business.movie.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/user/{id}")
    public UserEntity findById(@PathVariable Long id){
        //访问被加密的路径时需要用到username和password 下面这行代码为restTemplate添加一个安全访问拦截器
        this.restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("user","123456"));
        return this.restTemplate.getForObject("http://MICROSERVICE-PROVIDER-USER/"+id,UserEntity.class);
    }

    @GetMapping("/user-instance")
    public List<ServiceInstance> showInfo(){
        return this.discoveryClient.getInstances("microservice-provider-user");
    }

}

启动服务后访问 eureka集群服务其中的一个主页可以看到如下信息

四全开eureka页面.jpg

现在访问 http ://192.168.10.250:8010/user/1 可以看到如下信息 注:不要直接复制地址 我加空格了 要不然他变成超链接。。

{
  id: 1,
  username: "account1",
  name: "张三",
  age: 20,
  balance: 100
}

到此 eureka服务集群 注册 调用已完成 。。。
我这个上面没说 其实 我这个 集成了Spring Boot Actuator 可以直接查一下
还可以直接点击 eureka服务器页面中的url 可以看到如下信息

{
  app: "microservice-provider-user",
  encoding: "UTF-8",
  java: {
    source: "1.8.0_131",
    target: "1.8.0_131"
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容