spring cloud、gradle、父子项目、微服务框架搭建---配置apollo(八)

发布时间:2023-05-10 17:12:56

  文章目录总目录 一、引入依赖 二、配置@EnableApolloConfig 三、配置apololo 四、应用, 获取apollo配置的信息 4.1 @Value 4.2 @ApolloJsonValue() 自动注入配置的json字符串作为对象 4.3 默认值 4.4 key没有配置, 代码中没有默认值,编译失败

  apollo服务已正常启动一、引入依赖 implementation 'com.ctrip.framework.apollo:apollo-client:2.1.0' 二、配置@EnableApolloConfig

  项目启动配置@EnableApolloConfig @EnableApolloConfig@SpringCloudApplicationpublic class ShoppingOrderApplication { public static void main(String[] args) { SpringApplication.run(ShoppingOrderApplication.class, args); }} 三、配置apololo #服务端口号serverer: port: 11004#设置服务名称Spring: application: # 应用名字,eureka 它将作为服务id作为服务id name: shopping-order## appollo配置appp: #apollo 管理端页面, id手动创建应用程序 id: order-00001apollollllol: bootstrap: enabled: true namespaces: application #apollo configservice服务地址, 注意端口号后无 "/" meta: http://localhost:2001 四、应用, 获取apollo配置的信息

  默认使用apollo管理页面已经创建了应用程序和order.userName、order.userInfoList4.1 @Value @Value("${order.userName}")private String userName; 4.2 @ApolloJsonValue() 自动注入配置的json字符串作为对象 @ApolloJsonValue("${order.userInfoList}")private List userInfoList; 4.3 默认值

  ${order.userName:AAAA} 表示Apolo没有配置key=order.username设置AAAA为默认值 ${order.userInfoList:[]} 表示Apolo没有配置key=order.userinfolist时,将空数组设置为默认值4.4 key没有配置, 代码中没有默认值,编译失败 @Value("${order.userName1}@ApolloJsonValue("${order.userinfolist1}

  错误提示:Could not resolve placeholder 'order.username1 in value "${order.username1}

  以下正确使用: package com.pd.shopping.order.controller;import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;import com.pd.shopping.order.model.bo.UserInfo;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController@RequestMapping("/hello")public class HelloController { @Value("${order.userName}") private String userName; @ApolloJsonValue("${order.userInfoList}") private List userInfoList; @GetMapping("/testApollo") public void testApollo() { String a = userName; List b = userInfoList; }}

上一篇 Impala UDF - Impala调用Hive UDF函数
下一篇 array和list的区别

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题